import { User } from "../../net/user";
import CCObject, { ArrData } from "../../eggy/cc_game/cc_object";

const { ccclass, property } = cc._decorator;


@ccclass
export default class CCTestBall extends CCObject {
    @property(cc.Label)
    lbUid: cc.Label = null;
    @property
    lifeMax = 0;
    life = 0;
    lifeCount = false;

    protected onDestroy(): void {
        console.log("ball destroy");
    }
    protected start(): void {
        this.lifeCount = this.lifeMax !== 0;
        this.life = this.lifeMax;
    }
    protected update(dt: number): void {
        this.node.x += this.dv.x * dt;
        this.node.y += this.dv.y * dt;
        if (this.lifeCount && User.userId == this.owner) {
            this.life -= dt;
            this.refresh();
            if (this.life < 0) {
                this.lifeCount = false;
                this.emit("die", this);
            }
        }
    }
    toArrData(): ArrData {
        let data = super.toArrData();
        data.push(this.life);
        return data;
    }
    fromArrData(data: ArrData): void {
        super.fromArrData(data);
        this.life = data.shift();

        this.refresh();
    }

    refresh() {
        this.lbUid.string = `${this.objUid}`;
    }

    static NewLobbyData(objName: string, owner: string, x: number, y: number, dX: number, dY: number, life: number) {
        let data = CCObject.NewArrData(objName, owner, x, y, dX, dY);
        data.push(life);
        return data;
    }
}